package dlut.alanhome.trafficview;


import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class videostream extends Activity implements  android.view.View.OnClickListener
{
    /** Called when the activity is first created. */
	int idx;
	Bitmap bm;
	ImageButton zoomin;
	ImageButton zoomout;
	Button up;
	Button down;
	ImageButton imgbtn;
	Button left;
	String strurl;
	Button right;
	snapshot ss;
	ImageView iv;
	int zoom=0;
	int v=1;
	int h=1;
	int showflag;
	boolean stopflag=false;
	Handler handler = new Handler()
	{		
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			iv.setImageBitmap(bm);
		}
	};
	Handler btnhandler = new Handler()
	{
		
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			disshowbutton();
		}
	};

	/** 
	* 按下这个按钮进行的颜色过滤 
	*/ 
	public final static float[] BT_SELECTED=new float[] { 
	2, 0, 0, 0, 2, 
	0, 2, 0, 0, 2, 
	0, 0, 2, 0, 2, 
	0, 0, 0, 1, 0 }; 

	/** 
	* 按钮恢复原状的颜色过滤 
	*/ 
	public final static float[] BT_NOT_SELECTED=new float[] { 
	1, 0, 0, 0, 0, 
	0, 1, 0, 0, 0, 
	0, 0, 1, 0, 0, 
	0, 0, 0, 1, 0 }; 

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,  
                      WindowManager.LayoutParams. FLAG_FULLSCREEN);  
        showflag=0;
        setContentView(dlut.alanhome.trafficview.R.layout.main1);
        idx = this.getIntent().getIntExtra("idx", 1);
        strurl = this.getIntent().getStringExtra("strurl");
        System.out.println("in video stream and get idx "+idx);
        iv  = (ImageView)findViewById(dlut.alanhome.trafficview.R.id.ImageView01);
                
        zoomin = (ImageButton)findViewById(dlut.alanhome.trafficview.R.id.Button02);
        zoomout = (ImageButton)findViewById(dlut.alanhome.trafficview.R.id.Button01);
        left = (Button)findViewById(dlut.alanhome.trafficview.R.id.Button03);
        right = (Button)findViewById(dlut.alanhome.trafficview.R.id.Button04);
        up = (Button)findViewById(dlut.alanhome.trafficview.R.id.Button05);
        down = (Button)findViewById(dlut.alanhome.trafficview.R.id.Button06);
        zoomin.setOnClickListener(this);  
        setButtonFocusChanged(zoomin);
        zoomout.setOnClickListener(this);
        setButtonFocusChanged(zoomout);
        left.setOnClickListener(this);
        up.setOnClickListener(this);
        down.setOnClickListener(this);
        right.setOnClickListener(this);
        
        new Thread(){
			public void run()
			{
				process();
			}
		}.start();
        
    }
    
    public void disshowbutton()
    {    	
    	showflag--;
    	System.out.println("showflag ++ :" +showflag );
		if(showflag==0){
			
    		zoomin.setVisibility(Button.INVISIBLE);
    		zoomout.setVisibility(Button.INVISIBLE);
    		left.setVisibility(Button.INVISIBLE);
    		down.setVisibility(Button.INVISIBLE);
    		up.setVisibility(Button.INVISIBLE);
    		right.setVisibility(Button.INVISIBLE);
		}
    }
    
    @Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		stopflag=true;
		try {
			ss.Standardize();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		stopflag=false;
	}

	public void showbutton()
	{
		zoomin.setVisibility(Button.VISIBLE);
		
    	zoomin.setFocusable(false);
    	zoomout.setVisibility(Button.VISIBLE);
    	left.setVisibility(Button.VISIBLE);
    	down.setVisibility(Button.VISIBLE);
    	up.setVisibility(Button.VISIBLE);
    	right.setVisibility(Button.VISIBLE);
    	showflag++;
    	System.out.println("showflag ++ :" +showflag );
    	new Thread()
    	{
    		public void run()
    		{
    			try {
    				Thread.sleep(6000);
    				
    					btnhandler.sendMessage(new Message());
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}.start();
	}
    @Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
    	showbutton();
    	
		return super.onTouchEvent(event);
		
	}

	void process()
    {	
    	ss = new snapshot();
 		ss.setusername("GoGeeks");
 		ss.setpasswd("ace.dutss@gmail.com");
 		try {
 			ss.Login();
 			ss.Standardize();
 			ss.Search();
 			while(true){
 				if(idx<2)
 					bm=ss.getPic(idx,"",320,480,1);
 				else 
 					bm= ss.getPic(idx, this.strurl, 320, 480, 3);
 				handler.sendMessage(new Message());
 				if(stopflag==true)
 					break;
 			}
 		} catch (Exception e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
    }
	public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener() { 

		@Override 
		public void onFocusChange(View v, boolean hasFocus) { 
		if (hasFocus) { 
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button02)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.rightbuttondown);
			}
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button01)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.leftbuttondown);
			}		
		} 
		else 
		{ 
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button02)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.rightbutton);
			}
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button01)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.leftbutton);
			}	
		} 
		} 
		}; 

		/** 
		* 按钮触碰按下效果 
		*/ 
		public final static OnTouchListener buttonOnTouchListener=new OnTouchListener() { 
		@Override 
		public boolean onTouch(View v, MotionEvent event) { 
		if(event.getAction() == MotionEvent.ACTION_DOWN){ 

			
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button01)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.leftbuttondown);
			}
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button02)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.rightbuttondown);
			}	
		} 
		else if(event.getAction() == MotionEvent.ACTION_UP){ 
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button01)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.leftbutton);
			}
			if(v.getId()==dlut.alanhome.trafficview.R.id.Button02)
			{
				((ImageButton)v).setImageResource(dlut.alanhome.trafficview.R.drawable.rightbutton);
			}	
		} 
		return false; 
		} 
		}; 

		/** 
		* 设置图片按钮获取焦点改变状态 
		* @param inImageButton 
		*/ 
		public final static void setButtonFocusChanged(View inView) 
		{ 
		inView.setOnTouchListener(buttonOnTouchListener); 
		inView.setOnFocusChangeListener(buttonOnFocusChangeListener); 
		} 
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case dlut.alanhome.trafficview.R.id.Button02:
			try {
				if(ss.zoomin()==false)
					Toast.makeText(this, "缩放已经到尽头",Toast.LENGTH_LONG).show();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				break;
			case dlut.alanhome.trafficview.R.id.Button01:
				try {
					if(ss.zoomout()==false)
						Toast.makeText(this, "缩放已经到尽头",Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case dlut.alanhome.trafficview.R.id.Button05:
			try {
				ss.left();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				break;
			case dlut.alanhome.trafficview.R.id.Button06:
			try {
				ss.right();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				break;
			case dlut.alanhome.trafficview.R.id.Button04:
			try {
				ss.up();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				break;
			case dlut.alanhome.trafficview.R.id.Button03:		
			try {
				ss.down();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			showbutton();
		}
	}
   
}
